//
//  HNSH_LogisticsViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/1.
//

#import "HNSH_LogisticsViewController.h"

//view
#import "HNSH_LogisticsTopView.h"
#import "HNSH_LogisticsCell.h"

@interface HNSH_LogisticsViewController ()

/* topView */
@property (nonatomic, strong) HNSH_LogisticsTopView *topView;

/* model */
@property (nonatomic, strong) HNSH_LogisticsModel *model;

@end

static NSString *HNSH_LogisticsCellID = @"HNSH_LogisticsCellID";

@implementation HNSH_LogisticsViewController

//MARK: Cycle Methods
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadNewData];
}

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"物流信息" showLeftItem:YES];
    
    [self addAllViews];
}

//MARK: Myself Methods
- (void)addAllViews{
    self.tableView.tableHeaderView = self.topView;
    [self.tableView registerClass:[HNSH_LogisticsCell class] forCellReuseIdentifier:HNSH_LogisticsCellID];
}

- (void)loadNewData{
    NSString * expressNo = self.orderModel.uid;
    if (!expressNo.length) {
        return;
    }
    [HNSH_ShopNetServer requestGoodsOrderShippingWithOrderId:expressNo code:self.orderModel.shipping_code success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSDictionary * data = responseObject.resultset;
            HNSH_LogisticsModel * model = [HNSH_LogisticsModel yy_modelWithJSON:data];
            self.model = model;
            self.topView.model = model;
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (HNSH_LogisticsTopView *)topView{
    if (!_topView) {
        _topView = [[HNSH_LogisticsTopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 174)];
        _topView.orderModel = self.orderModel;
    }
    return _topView;
}

//MARK: UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_LogisticsCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_LogisticsCellID];
    cell.model = self.model.data[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
